// pages/msg_staff_add/msg_staff_add.js
let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        LISLIZHI: false,
        array1: [{
            'LCODE': '1',
            'LNAME': '一车间'
        },
        {
            'LCODE': '2',
            'LNAME': '二车间'
        },
        {
            'LCODE': '3',
            'LNAME': '三车间'
        },
        {
            'LCODE': '4',
            'LNAME': '四车间'
        },
        {
            'LCODE': '5',
            'LNAME': '五车间'
        },
    ],
        LUSERCODE:'',
        LROLENAME:'',
        LROLEID:'',
        LISLIZHI:false,
        LUSERNAME:'',
        LFARM:'',
        LMOBILE:'',
        LREMARK:'',
    },
    switch1Change: function (e) {
        this.setData({
            LISLIZHI: e.detail.value
        })
    },
    bindPickerChange1(e) { // 品质
        this.setData({
            index1: e.detail.value
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.setData({
                    LFARM: this.data.array1[i].LCODE,
                })
            }
        }
    },
    bindPickerChange2(e){
        this.setData({
            index2: e.detail.value
        })
        for (var i = 0; i < this.data.array2.length; i++) {
            if (i == e.detail.value) {
                this.setData({
                    LROLENAME: this.data.array2[i].LROLENAME,
                    LROLEID:this.data.array2[i].LROLECODE
                })
            }
        }
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.LUSERNAME = e.detail.value
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.LFARM = e.detail.value
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.LDEPARTMENTID = e.detail.value
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.LMOBILE = e.detail.value
        } else if (e.currentTarget.dataset.id == '5') {
            this.data.LREMARK = e.detail.value
        }
    },
    update() { // 提交
        let that = this
        let table = {
            "table": [{
                'LUSERCODE': that.data.LUSERCODE, // 员工号
                'LUSERNAME': that.data.LUSERNAME, //名称
                'LFARM': that.data.LFARM, //车间
                'LROLENAME': that.data.LROLENAME, //职位
                'LROLEID': that.data.LROLEID, //职位ID
                'LMOBILE': that.data.LMOBILE, //电话
                'LREMARK': that.data.LREMARK, //备注
                'LISLIZHI': that.data.LISLIZHI, //是否离职
            }]
        }
        wx.request({
            url: 'https://' + app.globalData.url + '/staff/addStaff',
            data: {
                "model": JSON.stringify(table),
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.success == true) {
                    wx.navigateBack({
                        delta: 1
                    })
                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: '添加失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            fail: function (res) {
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        app.globalData.userinfo = ''
        wx.request({
            url: 'https://' + app.globalData.url + '/Process/findStaffRole',
            data: {
           
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: function (res) {
                if (res.data.success == true) {
                    that.setData({
                        array2: res.data.obj
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {

                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
        wx.request({
            url: 'https://' + app.globalData.url + '/machineInfo/getCode',
            data: {
                RuleId: 8
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: function (res) {
                if (res.data.success == true) {
                    that.setData({
                        LUSERCODE: res.data.obj
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取员工号失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})